(async () => {
  const delay = time => new Promise(resolve => setTimeout(resolve, time));

  let miningBox = document.querySelector('.mining-box');
  if (!miningBox) {
    console.log('未登录游戏');
  }

  while (1) {
    const isDesktop = window.innerWidth > 980;
    miningBox = isDesktop
      ? document.querySelector('.game--container > .mining-box')
      : document.querySelector('.game--header .mining-box');
    const miningTimer = miningBox.querySelector('.mining-box--timer');
    const miningBtn = miningBox.querySelector('.start-mining-button');
    if (miningBtn) {
      // 可以挖矿
      miningBtn.click();
      await delay(1000);
      const confirmBtn = miningBox.querySelector('.modal-wrapper .modal-bottom .button');
      if (confirmBtn) {
        confirmBtn.click();
        console.log('成功执行，等待下一轮');
        await delay(20000);
      }
    } else {
      // 检查剩余时间
      const getRestSeconds = str => {
        const times = str.split(' ');
        return times.reduce((total, cur) => {
          const reg = /\d+/;
          const timeMap = { h: 3600, m: 60, s: 1 };
          Object.keys(timeMap).forEach(flag => {
            if (cur.includes(flag)) {
              total += Number(reg.exec(cur)[0]) * timeMap[flag];
            }
          });
          return total;
        }, 0);
      };
      const restSeconds = getRestSeconds(miningTimer.querySelector('.timer-the-time').textContent.trim());
      const maxSeconds = 3 * 60;
      let actualDelaySeconds;
      if (!restSeconds || Number.isNaN(restSeconds) || restSeconds > maxSeconds) {
        actualDelaySeconds = maxSeconds;
      } else {
        actualDelaySeconds = restSeconds + 10; // 10秒buffer
      }
      console.log(`等待${actualDelaySeconds}s继续执行`);
      await delay(actualDelaySeconds * 1000);
    }
  }
})()